import 'package:dio/dio.dart';

class NetworkService {

  static final BaseOptions options = BaseOptions(
      baseUrl: 'https://www.wanandroid.com',
      connectTimeout: const Duration(seconds: 15),
      sendTimeout: const Duration(seconds: 15),
      receiveTimeout: const Duration(seconds: 15),
      //内容类型
      contentType: 'application/json;Charset=UTF-8',
      //响应数据类型：Json
      responseType: ResponseType.json);

  final Dio _dio = Dio(options);

  Future<Response> get(String url,
      {Map<String, dynamic>? queryParameters}) async {
    try {
      return await _dio.get(url, queryParameters: queryParameters);
    } catch (e) {
      throw Exception('Error during GET request: $e');
    }
  }

  Future<Response> post(String url, dynamic data) async {
    try {
      return await _dio.post(url, data: data);
    } catch (e) {
      throw Exception('Error during POST request: $e');
    }
  }

  Future<Response> put(String url, dynamic data) async {
    try {
      return await _dio.put(url, data: data);
    } catch (e) {
      throw Exception('Error during PUT request: $e');
    }
  }

  Future<Response> delete(String url) async {
    try {
      return await _dio.delete(url);
    } catch (e) {
      throw Exception('Error during DELETE request: $e');
    }
  }
}
